package com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.ResponseCode;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.entity.CasualtiesDE;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.entity.Statistics;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.param.CasualtiesDECalculationParam;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.param.SysDataVersionPageParam;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.service.CasualtiesDEService1;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.vo.EvaluateViewVO;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.vo.UpdateVO;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.param.AssessParam;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import com.css.fxfzfxqh.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/06/08
 * @Description: 人员伤亡确定评估
 */
@Slf4j
@RestController
@RequestMapping("/casualtiesDE")
public class CasualtiesDEController {
    @Resource
    private CasualtiesDEService1 casualtiesDEService1;

    /**
     * 新增评估
     *
     * @param param 参数
     * @return
     */
    @PostMapping("/addAssess")
    public RestResponse addAssess(@RequestBody CasualtiesDE param) throws Exception{
        casualtiesDEService1.addAssess(param);
        return RestResponse.succeed("保存成功");

    }

    /**
     * 查看
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String id) {
        RestResponse response = null;
        try {
            EvaluateViewVO vehManage=casualtiesDEService1.findById(id);
            response = RestResponse.succeed(vehManage);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 修改评估
     *
     * @param param 参数
     * @return
     */
    @PostMapping("/upDateAssess")
    public RestResponse upDateAssess(@RequestBody CasualtiesDE param) {
        try {
            casualtiesDEService1.upDateAssess(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改评估失败!", e);
            return RestResponse.fail("修改失败!");
        }
    }

    /**
     * 获取评估code
     */
    @PostMapping("/getCode")
    public RestResponse getCode(@RequestBody CasualtiesDE param) {
        try {
            String code = casualtiesDEService1.getCode(param);
            return RestResponse.succeed(code);
        } catch (Exception e) {
            String errormessage = "生成code失败";
            log.error(errormessage, e);
            return RestResponse.fail("获取失败");
        }

    }

    /**
     * 分页列表
     *
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, AssessParam param) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = casualtiesDEService1.getPage(curPage, pageSize, param);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 计算
     *
     * @param map 参数
     * @return
     */
    @PostMapping("/calculation")
    public RestResponse calculation(@RequestBody Map<String,String> map) {
        try{
            casualtiesDEService1.calculation(map.get("taskId"),map.get("seismicInputMode"));
            return ResponseCreator.createSuccessResponse(null,"评估完成");
        }catch (Exception e){
            return ResponseCreator.createFailResponse(e.getMessage(), ResponseCode.EVALUATE_ERROR);
        }
    }

    /**
     * 地震输入方式是影响场时，格网可能存在跨越烈度的情况，需要根据格网在不同烈度圈中的相交面积来决定
     * 格网属于哪个烈度，所以在calculation接口操作时只是存的临时接口需要调用该接口计算得到最终结果
     * @param map 参数  intensityVersion是影响场的版本，assessId是评估记录的id
     * @return
     */
    @PostMapping("/calculationIntensity")
    public RestResponse calculationIntensity(@RequestBody Map<String,String> map) throws Exception{
        casualtiesDEService1.calculationIntensity(map.get("intensityVersion"),map.get("assessId"));
        return ResponseCreator.createSuccessResponse(null,"评估完成");
    }


    /**
     * 导入
     * @param id   评估记录id
     * @return
     */
    @PostMapping("/importExcel")
    public RestResponse reportImport(HttpServletRequest req, String id) throws Exception{
        //获取上传到服务器的临时文件对象
        Part part = req.getPart("file");
        //获取临时文件的输入流
        InputStream inputStream = part.getInputStream();
        String str = casualtiesDEService1.reportImport(inputStream,id);
        return RestResponse.succeed(str);
    }

    /**
     * 导出
     *
     * @param param 参数
     */
    @PostMapping("/exportData")
    public void exportData(HttpServletResponse response, CasualtiesDECalculationParam param) {
        casualtiesDEService1.exportData(response, param);
    }

    /**
     * 查询计算值
     *
     * @param seismicInputMode 地震输入方式0烈度,1四个概率水准,2地震影响场
     * @param id               评估记录id
     * @param condition        条件(地震烈度 或 四个概率水准)
     * @param casualties     人员伤亡(死亡重伤)
     * @param time     时间段(白天晚上)
     * @return
     */
    @GetMapping("/queryCalculation")
    public RestResponse queryCalculation(String seismicInputMode, String id, String condition, String casualties,String time, HttpServletRequest request) {
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = casualtiesDEService1.queryCalculation(curPage,pageSize,seismicInputMode, id, condition, casualties,time);
            return RestResponse.succeed(page);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 修改经验评估与修正值
     *
     * @return
     */
    @PostMapping("/updateCorrectionValue")
    public RestResponse updateCorrectionValue(@RequestBody UpdateVO vo) {
        RestResponse restResponse = null;
        try {
            casualtiesDEService1.updateCorrectionValue(vo.getId(),vo.getCorrectionValue(),vo.getTime(),vo.getCasualties());
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 查询人口版本数据列表
     *
     * @param param
     * @return
     */
    @GetMapping("/versionPage")
    public RestResponse<?> versionPage(SysDataVersionPageParam param) {
        try {
            JSONObject jsonObject = casualtiesDEService1.versionPage(param);
            return RestResponse.succeed(jsonObject);
        } catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail("查询失败");
        }
    }

    /**
     * 查询计算值
     *
     * @param seismicInputMode 地震输入方式0烈度,1四个概率水准,2地震影响场
     * @param taskId               评估记录id
     * @param condition        条件(地震烈度 或 四个概率水准)
     * @param casualties     人员伤亡(死亡重伤)
     * @param time     时间段(白天晚上)
     * @return
     */
    @GetMapping("/statistics")
    public RestResponse statistics(String seismicInputMode, String taskId, String condition, String casualties,String time) throws Exception{
        List<Statistics> list = casualtiesDEService1.statistics(seismicInputMode,taskId,condition,casualties,time);
        return ResponseCreator.createSuccessResponse(list);
    }

}
